﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Document;

namespace TestEditor
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		FoldingManager foldingManager;
		AbstractFoldingStrategy foldingStrategy;
		CompletionWindow completionWindow;

		public MainWindow()
		{
			InitializeComponent();
			//textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
			textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".js");


			foldingManager = FoldingManager.Install(textEditor.TextArea);
			//foldingStrategy = new XmlFoldingStrategy();
			foldingStrategy = new BraceFoldingStrategy();
			foldingStrategy.UpdateFoldings(foldingManager, textEditor.Document);

			textEditor.TextChanged += new EventHandler(textEditor_TextChanged);

			textEditor.TextArea.TextEntering += new TextCompositionEventHandler(TextArea_TextEntering);
			textEditor.TextArea.TextEntered += new TextCompositionEventHandler(TextArea_TextEntered);
		}

		void TextArea_TextEntered(object sender, TextCompositionEventArgs e)
		{
			if (e.Text == ".")
			{
				// Open code completion after the user has pressed dot:
				completionWindow = new CompletionWindow(textEditor.TextArea);
				IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
				data.Add(new MyCompletionData("First"));
				data.Add(new MyCompletionData("Second"));
				data.Add(new MyCompletionData("Third"));
				completionWindow.Show();
				completionWindow.Closed += (a, b) => completionWindow = null;

			}
		}

		void TextArea_TextEntering(object sender, TextCompositionEventArgs e)
		{
			if (e.Text.Length > 0 && completionWindow != null)
			{
				if (!char.IsLetterOrDigit(e.Text[0]))
				{
					// Whenever a non-letter is typed while the completion window is open,
					// insert the currently selected element.
					completionWindow.CompletionList.RequestInsertion(e);
				}
			}
			// Do not set e.Handled=true.
			// We still want to insert the character that was typed.
		}

		void textEditor_TextChanged(object sender, EventArgs e)
		{
			foldingStrategy.UpdateFoldings(foldingManager, textEditor.Document);
		}
	}

	public class MyCompletionData : ICompletionData
	{
		public MyCompletionData(string text)
		{
			this.Text = text;
		}

		public void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs)
		{
			textArea.Document.Replace(completionSegment, this.Text);
		}

		public object Content
		{
			get { return this.Text; }
		}

		public object Description
		{
			get { return "Description for " + this.Text; }
		}

		public ImageSource Image
		{
			get { return null; }
		}

		public double Priority
		{
			get { return 0; }
		}

		public string Text { get; private set; }
	}
}
